import xml.dom.minidom;
import re;
import datetime;
import gadfly;

def convert_month(month):
	if (month == "Jan"):
		return "01";
	if (month == "Feb"):
		return "02";
	if (month == "Mar"):
		return "03";
	if (month == "Apr"):
		return "04";
	if (month == "May"):
		return "05";
	if (month == "Jun"):
		return "06";
	if (month == "Jul"):
		return "07";
	if (month == "Aug"):
		return "08";
	if (month == "Sep"):
		return "09";
	if (month == "Oct"):
		return "10";
	if (month == "Nov"):
		return "11";
	if (month == "Dec"):
		return "12";
	return "00";
	
def find_target_dates(max):
	today = datetime.date.today();
	target_dates = [];
	for x in range(1, max+1):
		delta = datetime.timedelta(days=(max-x));
		target = today - delta;
		target_dates.append(target);
	return target_dates;

def parse_rss(filename):
	print "Parsing", filename+"...";
	dom = xml.dom.minidom.parse(filename);
	feed_name = dom.getElementsByTagName("channel").item(0).getElementsByTagName("title").item(0).firstChild.data;
	items = dom.getElementsByTagName("channel").item(0).getElementsByTagName("item");
	wins = [];
	for i in items:
		if (feed_name == "Manga Jouhou - Releases"):
			if (i.getElementsByTagName("manganews:torrent").item(0).firstChild == None):
				pass;
			elif (i.getElementsByTagName("manganews:torrent").item(0).firstChild.data.find("torrent") < 0):
				pass;
			else:
				fake_title= i.getElementsByTagName("title").item(0).firstChild.data;
				template = re.compile("(.+) Volume \d+ Chapter \d+");
				m = template.match(fake_title);
				if (m != None):
					title = m.group(1);
					torrent = i.getElementsByTagName("manganews:torrent").item(0).firstChild.data;
					volume = i.getElementsByTagName("manganews:volume").item(0).firstChild.data;
					chapter = i.getElementsByTagName("manganews:chapter").item(0).firstChild.data;
					group = i.getElementsByTagName("manganews:scanlators").item(0).getElementsByTagName("manganews:scanlator").item(0).getElementsByTagName("manganews:name").item(0).firstChild.data;
					fake_pubdate = i.getElementsByTagName("pubDate").item(0).firstChild.data;
					template = re.compile(".+, (.+) (.+) (.+) .+:.+:.+ GMT");
					m = template.match(fake_pubdate);
					date = m.group(1);
					month = convert_month(m.group(2));
					year = m.group(3);
					pubdate = year+"-"+month+"-"+date;
					details = [];
					details.append(title);
					details.append(torrent);
					details.append(volume);
					details.append(chapter);
					details.append(group);
					details.append(pubdate);
					wins.append(details);
		elif (feed_name == "Baka Updates Manga"):
			link = i.getElementsByTagName("link").item(0).firstChild.data;
			#print link.find("torrent");
			if (link.find("torrent") >= 0):
				#print "\t"+link;
				info = i.getElementsByTagName("title").item(0).firstChild.data;
				pubdate = i.getElementsByTagName("description").item(0).firstChild.data;
				template = re.compile('\[(.+)\](.+) v\.(.+) c\.(.+)');
				m = template.match(info);
				if (m != None):
					group = m.group(1);
					title = m.group(2);
					volume = m.group(3);
					chapter = m.group(4);
					details = [];
					details.append(title);
					details.append(link);
					details.append(volume);
					details.append(chapter);
					details.append(group);
					details.append(pubdate);
					#print details;
					wins.append(details);
				else:
					template = re.compile("\[(.+)\](.+) v\.(.+)");
					m = template.match(info);
					if (m != None):
						group = m.group(1);
						title = m.group(2);
						volume = m.group(3);
						chapter = "00";
						details = [];
						details.append(title);
						details.append(link);
						details.append(volume);
						details.append(chapter);
						details.append(group);
						details.append(pubdate);
						wins.append(details);
					else:
						template = re.compile("\[(.+)\](.+) c\.(.+)");
						m = template.match(info);
						group = m.group(1);
						title = m.group(2);
						volume = "00";
						chapter = m.group(3);
						details = [];
						details.append(title);
						details.append(link);
						details.append(volume);
						details.append(chapter);
						details.append(group);
						details.append(pubdate);
						wins.append(details);
		elif (feed_name == "mahou All Manga RSS Feed"):
			link = i.getElementsByTagName("link").item(0).firstChild.data;
			if (link.find("torrent") < 0):
				pass;
			else:
				info = i.getElementsByTagName("title").item(0).firstChild.data;
				fake_pubdate = i.getElementsByTagName("pubDate").item(0).firstChild.data;
				template = re.compile(".+, (\d+) (.+) (\d+) \d+:\d+:\d+ GMT");
				m = template.match(fake_pubdate);
				date = m.group(1);
				month = convert_month(m.group(2));
				year = m.group(3);
				pubdate = year+"-"+month+"-"+date;
				group = "mahou";
				template = re.compile("(.+) (v(\d+)c(\d+)|v(\d+)) \(.+\)");
				m = template.match(info);
				title = m.group(1);
				volume = m.group(3);
				chapter = m.group(4);
				if (volume == None or chapter == None):
					volume = m.group(5);
					chapter = "00";
				details = [];
				details.append(title);
				details.append(link);
				details.append(volume);
				details.append(chapter);
				details.append(group);
				details.append(pubdate);
				wins.append(details);
	print "Done.";
	return wins;
	
def parse_html(filename):
	print "Parsing " + filename + "..."
	data = open(filename).read();
	if (filename == "aw-html"):
		start = data.find("<div id=\"latest-releases\">");
		end = data.find("<div id=\"more-releases\">");
		chunk = data[start:end];
		template = re.compile("<li>\s*<div class=\"release-title\"><a href=\"index.php\?page=.+\" title=\".*\">(.*)</a> (.*)</div>\s*<div class=\"release\-date\">(.*)</div>\s*<div class=\"release\-links\">\s*<span id=\"link\-irc\"><a class=\"release\-links\" href=\"index.php\?page=botlist\" title=\"Available on our IRC Channel\">IRC</a></span>\s*<span id=\"link\-bt\"><a class=\"release\-links\" a href=\"(.*)\" title=\"Download using BitTorrent\">BT</a></span>\s*</div>\s*</li>");
		m = template.finditer(chunk);
		wins = [];
		for k in m:
			title = k.group(1);
			chapter =  k.group(2);
			volume = "00";
			group = "AnimeWaves";
			pubdate = k.group(3);
			torrent = "http://animewaves.net/"+k.group(4);
			wins.append([title, torrent, volume, chapter, group, pubdate]);
	print "Done.";
	return wins;
	
def html_out(huge_list):
	print "Writing HTML...";
	mti_out = open("mti.html", 'w');
	mti_out.write("<html>\t<title>Manga Torrent Index</title>\n\t<body>\n");
	for [title, link, volume, chapter, group, pubdate] in huge_list:
		mti_out.write("\t\t" + title + "<br />\n");
		mti_out.write("\t\tVolume " + volume + " Chapter " + chapter + " <br />\n");
		mti_out.write("\t\t" + group + "<br />\n");
		mti_out.write("\t\t" + pubdate + "<br />\n");
		mti_out.write("\t\t" + link + "<br />\n");
		mti_out.write("\t\t<br />\n");
	mti_out.write("\t</body>\n</html>");
	mti_out.close();
	print "Done";
	
def rss_out(huge_list):
	print "Writing RSS...";
	mti_out = open("mti.rss", 'w');
	mti_out.write("<?xml version='1.0' encoding='ISO-8859-1' ?>\n");
	mti_out.write("<rss version='2.0'>\n");
	mti_out.write("<channel>");
	mti_out.write("<title>Manga Torrent Index</title>");
	mti_out.write("<link>...</link>");
	for [title, link, volume, chapter, group, pubdate] in huge_list:
		mti_out.write("<item>\n");
		mti_out.write("<title>"+title+"</title>\n");
		mti_out.write("<link>"+link+"</link>\n");
		mti_out.write("</item>\n");
	mti_out.write("</channel>\n");
	mti_out.write("</rss>");
	mti_out.close();
	print "Done.";
	
def rss_ep_out(huge_list):
	print "Writing RSS_ep...";
	mti_out = open("mti.rss", 'w');
	mti_out.write("<?xml version='1.0' encoding='ISO-8859-1' ?>\n");
	mti_out.write("<rss version='2.0'>\n");
	mti_out.write("<channel>");
	mti_out.write("<title>Manga Torrent Index</title>");
	mti_out.write("<link>...</link>");
	for [title, link, volume, chapter, group, pubdate] in huge_list:
		#print title, link, volume, chapter, group, pubdate;
		#print pubdate;
		mti_out.write("<item>\n");
		smart_title = title + " " + volume + "x" + chapter;
		mti_out.write("<title>"+smart_title+"</title>\n");
		mti_out.write("<link>"+link+"</link>\n");
		mti_out.write("</item>\n");
	mti_out.write("</channel>\n");
	mti_out.write("</rss>");
	mti_out.close();
	print "Done.";

def add_list_to_db(conn, list):
	print "DBing...";
	cursor = conn.cursor();
	interstat = "insert into entries(title, link, volume, chapter, m_group, pubdate) values (?, ?, ?, ?, ?, ?)";
	for [title, link, volume, chapter, group, pubdate] in list:
		cursor.execute("select * from entries where title='"+title+"'");
		results = cursor.fetchall();
		fail = 0;
		if (len(results) > 0):
			cursor.execute("select * from entries where title='"+title+"' and volume='"+volume+"'");
			results = cursor.fetchall();
			if (len(results) > 0):
				cursor.execute("select * from entries where title='"+title+"' and volume='"+volume+"' and chapter='"+chapter+"'");
				results = cursor.fetchall();
				if (len(results) > 0):
					fail = 1;
		if (fail == 0):
			cursor.execute(interstat, (title, link, volume, chapter, group, pubdate));
	conn.commit();

def print_db(conn):
	cursor = conn.cursor();
	cursor.execute("select * from entries");
	print cursor.pp();
	conn.commit();

def get_list_from_db(conn, dates):
	cursor = conn.cursor();
	wins_list = [];
	for x in dates:
		interstat = "select * from entries where pubdate='"+str(x)+"'";
		cursor.execute(interstat);
		for y in cursor.fetchall():
			wins_list.append([y[2], y[4], y[3], y[0], y[5], y[1]]);
	return wins_list;

if __name__ == '__main__':
	mn_list = parse_rss("mn-rss");
	mu_list = parse_rss("mu-rss");
	ma_list = parse_rss("ma-rss");
	aw_list = parse_html("aw-html");
	conn = gadfly.gadfly("mti_db", ".");
	add_list_to_db(conn, mu_list);
	add_list_to_db(conn, mn_list);
	add_list_to_db(conn, ma_list);
	add_list_to_db(conn, aw_list);
	win_list = get_list_from_db(conn, find_target_dates(5));
	conn.close();
	
	html_out(win_list);
	rss_ep_out(win_list);
	print "All done.";
